﻿// HandWheel.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "HandWheel.h"
#include "liwishell.h"
#include "Data.h"

// HandWheel 对话框

IMPLEMENT_DYNAMIC(HandWheel, CDialogEx)

HandWheel::HandWheel(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_HandWheel, pParent)
{

}

HandWheel::~HandWheel()
{
}

void HandWheel::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(HandWheel, CDialogEx)
	ON_BN_CLICKED(IDC_connectWheel, &HandWheel::OnBnClickedconnectwheel)
END_MESSAGE_MAP()


// HandWheel 消息处理程序


BOOL HandWheel::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	GetDlgItem(IDC_Mag)->SetWindowTextW(_T("1.00"));
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void HandWheel::OnBnClickedconnectwheel()
{
	// TODO: 在此添加控件通知处理程序代码
	CString name;
	GetDlgItem(IDC_connectWheel)->GetWindowTextW(name);
	if (name == "手轮连接")
	{
		int connectAxis = -1;	//轴标志
		int enable = 0;			//使能标志
		for (int i = 0; i < 3; i++)
		{
			if (((CButton*)GetDlgItem(IDC_Axis0 + i))->GetCheck())
			{
				PMC_GetAxisEnable(Data::handle, i, &enable);
				if (enable == 0)
				{
					MessageBox(_T("轴未使能，请先对轴使能"));
					return;
				}
				connectAxis = i;
			}
		}

		int type = ((CButton*)GetDlgItem(IDC_ABWheel))->GetCheck() ? 3 : 6;
		PMC_SetAtype(Data::handle, 3, type);
		//连接
		CString ratio;
		GetDlgItem(IDC_Mag)->GetWindowTextW(ratio);
		int ret = PMC_Connect(Data::handle, _ttof(ratio), 3, connectAxis);
		if (ret != 0)
		{
			MessageBox(_T("手轮连接失败"));
			return;
		}
		((CButton*)GetDlgItem(IDC_connectWheel))->SetWindowTextW(_T("手轮断开"));

	}
	else
	{
		//取消同步运动
		int ret = PMC_Single_Cancel(Data::handle, 3, 2);
		if (ret!=0)
		{
			MessageBox(_T("手轮断开失败"));
			return;
		}
		((CButton*)GetDlgItem(IDC_connectWheel))->SetWindowTextW(_T("手轮连接"));
	}

}



